﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Critr
{
    public partial class CritrMainForm : Form
    {
        private LogSheet logsheet;

        public CritrMainForm()
        {
            InitializeComponent();
            LogEntriesDataGridView.AutoGenerateColumns = true;
            LogEntriesDataGridView.Visible = false;
            LogEntriesDataGridView.CellEndEdit += LogEntriesDataGridViewChanged;
        }

        private void openLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream logStream = null;
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.InitialDirectory = "C:\\";
            ofd.Filter = "Excel 2010 Files (*.xlsx)|*.xlsx";
            ofd.RestoreDirectory = true;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((logStream = ofd.OpenFile()) != null)
                    {
                        using (logStream)
                        {
                            logsheet = LogParser.ParseExcelLog(ofd.FileName);
                            FillLogTab();
                            UploadButton.Visible = true;
                            LogEntriesDataGridView.Visible = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("ERROR: File I/O error. Message: " + ex.Message);
                }
            }
        }

        private void FillLogTab()
        {
            ProgrammerLabel.Text =      logsheet.Programmer;
            BroadcastDateLabel.Text =   logsheet.BroadcastDate.ToString();
            NameOfShowLabel.Text =      logsheet.NameOfShow;
            GuestLabel.Text =           logsheet.Guest;
            TimeLabel.Text =            logsheet.Time;
            DescriptionLabel.Text =     logsheet.Description;

            UpdateStatusStrip();
            RefreshComponents();
        }

        private void UpdateStatusStrip() 
        {
            EntriesLabel.Text = logsheet.Entries.Count.ToString();
            CanconLabel.Text =          string.Format("{0:N2}%", ((double)logsheet.Entries.Count(entry => entry.Cancon == true) / (double)logsheet.Entries.Count) * 100);
            HitLabel.Text =             string.Format("{0:N2}%", ((double)logsheet.Entries.Count(entry => entry.Hit == true) / (double)logsheet.Entries.Count) * 100);
            ChartLabel.Text =           string.Format("{0:N2}%", ((double)logsheet.Entries.Count(entry => entry.Chart == true) / (double)logsheet.Entries.Count) * 100);
        }

        private void LogEntriesDataGridViewChanged(object sender, EventArgs e)
        {
            UpdateStatusStrip();
        }

        private void RefreshComponents()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = logsheet.Entries;
            LogEntriesDataGridView.DataSource = bs;
            LogEntriesDataGridView.Refresh();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UploadButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(String.Format("Are you sure you want to insert {0} rows into the database?", logsheet.Entries.Count), 
                "Save to Database", 
                MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    logdb.Insert(logsheet);
                    MessageBox.Show(String.Format("{0} rows successfully inserted.", logsheet.Entries.Count));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("An error occurred updating the database. Message: {0}", ex.Message));
                }
            }
        }

        private void newQueryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form dqf = new DatabaseQueryForm();
            dqf.Show();
        }
    }
}
